package com.leetcode.partition5;

import java.util.HashMap;
import java.util.Map;

/**
 * @author `RKC`
 * @date 2021/9/13 10:13
 */
public class LC447回旋镖的数量 {

    public static int numberOfBoomerangs(int[][] points) {
        return hashtable(points);
    }

    public static void main(String[] args) {
        int[][] points = {{1, 1}, {2, 2}, {3, 3}};
        System.out.println(numberOfBoomerangs(points));
    }

    private static int hashtable(final int[][] points) {
        int answer = 0;
        for (int[] point1 : points) {
            //统计两点的距离放入哈希表中
            Map<Integer, Integer> ht = new HashMap<>();
            for (int[] point2 : points) {
                //计算两点的距离
                int dis = (point1[0] - point2[0]) * (point1[0] - point2[0]) + (point1[1] - point2[1]) * (point1[1] - point2[1]);
                ht.put(dis, ht.getOrDefault(dis, 0) + 1);
            }
            //point1到其它所有点的距离已经统计完毕，因为要讲究顺序，根据排列组合公式，answer += m * (m - 1)
            for (Map.Entry<Integer, Integer> entry : ht.entrySet()) {
                int count = entry.getValue();
                answer += count * (count - 1);
            }
        }
        return answer;
    }
}
